package cn.harry.wx.service.impl;

import cn.harry.wx.dao.WxQrCodeDao;
import cn.harry.wx.entity.WxQrCode;
import cn.harry.wx.form.WxQrCodeForm;
import cn.harry.wx.service.MsgReplyService;
import cn.harry.wx.service.WxQrCodeService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;

/**
 * 公众号带参二维码
 *
 * @author honghh
 * Date 2021-04-08 16:19:27
 * Copyright (C) www.tech-harry.cn
 */
@Service("wxQrCodeService")
@RequiredArgsConstructor
public class WxQrCodeServiceImpl extends ServiceImpl<WxQrCodeDao, WxQrCode> implements WxQrCodeService {
    private final WxMpService wxService;
    @Autowired
    MsgReplyService msgReplyService;
    @Override
    public IPage<WxQrCode> getPage(WxQrCode wxQrCode, Integer pageSize, Integer pageNum) {
        LambdaQueryWrapper<WxQrCode> wrapper = new LambdaQueryWrapper<>(wxQrCode);
        if (StrUtil.isNotEmpty(wxQrCode.getBeginTime())) {
            wrapper.gt(WxQrCode::getCreateTime, wxQrCode.getBeginTime());
        }
        if (StrUtil.isNotEmpty(wxQrCode.getEndTime())) {
            wrapper.lt(WxQrCode::getCreateTime, wxQrCode.getEndTime());
        }
        return page(new Page<>(pageNum, pageSize), wrapper);
    }


    @Override
    public WxQrCode selectById(Long id) {
        return this.baseMapper.selectById(id);
    }
    
    @Override
    public int create(WxQrCode wxQrCode) {
        return this.baseMapper.insert(wxQrCode);
    }

 
    @Override
    public int update(Long id, WxQrCode wxQrCode) {
        wxQrCode.setId(id);
        return this.baseMapper.updateById(wxQrCode);
    }


    @Override
    public int deleteByIds(Long[] ids) {
        return this.baseMapper.deleteBatchIds(Arrays.asList(ids));
    }
    /**
     * 创建公众号带参二维码
     *
     * @param appid
     * @param form
     * @return
     */
    @Override
    public WxMpQrCodeTicket createQrCode(String appid, WxQrCodeForm form) throws WxErrorException {
        WxMpQrCodeTicket ticket;
        if (form.getIsTemp()) {//创建临时二维码
            ticket = wxService.getQrcodeService().qrCodeCreateTmpTicket(form.getSceneStr(), form.getExpireSeconds());
        } else {//创建永久二维码
            ticket = wxService.getQrcodeService().qrCodeCreateLastTicket(form.getSceneStr());
        }
        WxQrCode wxQrCode = new WxQrCode(form, appid);
        wxQrCode.setTicket(ticket.getTicket());
        wxQrCode.setUrl(ticket.getUrl());
        if (form.getIsTemp()) {
            wxQrCode.setExpireTime(new Date(System.currentTimeMillis() + ticket.getExpireSeconds() * 1000L));
        }
        this.save(wxQrCode);
        return ticket;
    }
    @Override
    public void sendQrCode(String fromUser, String appid) throws WxErrorException {
        WxMpQrCodeTicket ticket = this.createQrCode(appid, new WxQrCodeForm(fromUser));
        String imgUrl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + ticket.getTicket();
        msgReplyService.replyText(fromUser, imgUrl);
    }

    
}